#!/usr/bin/env python

import subprocess
import os


# Get latest database from server

print "Getting Latest Database..."

path_to_engine = "~/Appz/google_appengine"

kinds = ['Event',
         'EventAdmin',
         'Condition',
         'ConditionComment',
         'ConditionOpinion']

for kind in kinds:
  if not os.path.isfile ( "latest_one_"+kind ):
    print "Downloading kind %s" % kind
    dump_cmd = path_to_engine+"/bulkloader.py --dump --kind="+kind+" --url=http://blackdropsofspleen.appspot.com/remote_api --filename=latest_one_"+kind+" --email=gouttonio@gmail.com ~/htdocs/blackdropsofspleen"
    os.system(dump_cmd)
  else:
    print "Skipping kind %s" % kind

restore_cmd = ""
for kind in kinds:
  print "Restoration of kind %s" % kind
  restore_cmd = path_to_engine+"/bulkloader.py --restore --kind="+kind+" --url=http://localhost:8080/remote_api --filename=latest_one_"+kind+" --app_id=blackdropsofspleen --email=gouttonio@gmail.com ~/htdocs/blackdropsofspleen"
  os.system(restore_cmd)
print "Restoring Done"

print "Removing garbage..."
rm_cmd = "rm *.sql3 && rm bulkloader-log*"
subprocess.call(rm_cmd, shell=True)
for kind in kinds:
  os.system("rm latest_one_"+kind)

print "Done ! ^^"
